package com.example.publish;


import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

@Component
public class PublishSubscribeSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public void send(int i) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        String msg = "消息是"+i;
        //消息发送，使用void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
        //但不指定routingKey。因为FanoutExchange类型的交换机，routingKey不起作用，它向所有的队列发送广播，只要队列绑定到该交换机即接受消息。
        rabbitTemplate.convertAndSend("PublishSubscribeExchange","",msg,correlationData);
        System.out.println("发送成功 "+new Date()+" "+msg);
    }
}
